package com.shawn.integeration.business.studentmanager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shawn.integeration.business.studentmanager.mapper.StudentMapper;
import com.shawn.integeration.business.studentmanager.model.Student;
import com.shawn.integeration.business.studentmanager.model.dto.StudentDTO;
import com.shawn.integeration.business.studentmanager.service.IStudentService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 学生信息Service业务层处理
 *
 * @author: shawn
 * @Date: 2023/12/28 13:50
 * @Version 1.0.0
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Override
    public PageDTO<Student> queryPageList(StudentDTO student) {
        // 1.1.分页条件
        Page<Student> page = Page.of(student.getPageNo(), student.getPageSize());

        // 1.2.排序条件
        if(StrUtil.isNotBlank(student.getSortBy())){
            page.addOrder(student.getIsAsc()?OrderItem.asc(student.getSortBy()):OrderItem.desc(student.getSortBy()));
        }

        // 2. 查询，查询的接口将封装到 page 对象中。
        this.page(page);

        // 3. 数据非空校验
        List<Student> users = page.getRecords();
        if(users == null || users.isEmpty()){
            return PageDTO.of(page.getTotal(), page.getPages(), Collections.emptyList());
        }
        // 4. 存在数据，进行实体类转换
        List<UserVO> list = BeanUtil.copyToList(users, UserVO.class);

        // 5. 封装返回结果
        return new PageDTO<>(page.getTotal(), page.getPages(), list);
    }
}
